﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 聚欣订单分析.PublicClass;

namespace 聚欣订单分析.OrderAna
{
    public partial class profit : System.Web.UI.Page
    {
        PLSQLClass PL = new PublicClass.PLSQLClass();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            double sd = 0;//税点
            double yfyj = 0;//应付佣金
            string zcrq = "";//支付日期
            double sfyj = 0;//实付佣金
            double wp = 0;//无票
            double pp = 0;//普票
            double san = 0;//3%
            double liu = 0;//6%
            double zyrj = 0;//自研软件
            double ss = 0;//13%
            double sl = 0;//16%
            if (e.NewValues["税点"] != null)
                sd = Convert.ToDouble(e.NewValues["税点"]);
            else
            {
                e.RowError = "税点不能为空！";
                return;
            }
            if (e.NewValues["应付佣金"] != null)
                yfyj = Convert.ToDouble(e.NewValues["应付佣金"]);
            if (e.NewValues["实付佣金"] != null)
                zcrq = e.NewValues["支付日期"].ToString();
            if (e.NewValues["实付佣金"] != null)
                sfyj = Convert.ToDouble(e.NewValues["实付佣金"]);
            if (e.NewValues["无票"] != null)
                wp = Convert.ToDouble(e.NewValues["无票"]);
            if (e.NewValues["普票"] != null)
                pp = Convert.ToDouble(e.NewValues["普票"]);
            if (e.NewValues["三"] != null)
                san = Convert.ToDouble(e.NewValues["三"]);
            if (e.NewValues["六"] != null)
                liu = Convert.ToDouble(e.NewValues["六"]);
            if (e.NewValues["十三"] != null)
                ss = Convert.ToDouble(e.NewValues["十三"]);
            if (e.NewValues["十六"] != null)
                sl = Convert.ToDouble(e.NewValues["十六"]);
            if (e.NewValues["自研软件"] != null)
                zyrj = Convert.ToDouble(e.NewValues["自研软件"]);
            if (!e.IsNewRow)
            {
                string strSql = "select * from tbl_lrb where DDID = " + e.Keys[0].ToString();
                DataTable dt = PL.SqlBind(strSql);
                double ddje = Convert.ToDouble(e.OldValues["订单金额"]);
                //除税金额 订单金额/(税点+1)
                double csjs = Math.Round(ddje / (sd + 1), 2);
                //除税成本 无票/1.06+无票*0.1+普票+三/1.03+六/1.06+自研软件/1.13+十三/1.13+十六/1.16
                double cscb = wp / 1.06 + wp * 0.1 + pp + san / 1.03 + liu / 1.06 + zyrj / 1.13 + ss / 1.13 + sl / 1.16;
                cscb = Math.Round(cscb, 2);
                //含税成本 无票/0.9+普票+三+六+自研软件+十三+十六
                double hscb = wp / 0.9 + pp + san + liu + zyrj + ss + sl;
                hscb = Math.Round(hscb, 2);
                //报表除税毛利 除税金额-除税成本
                double csml = csjs - cscb;
                //市场含税毛利 订单金额-含税成本
                double hsml = ddje - hscb;
                string updateSql = "INSERT INTO [tbl_lrb] ([DDID],[税点],[除税金额],[应付佣金],[支付日期],[实付佣金],[无票],[普票],[三],[六],[自研软件],[十三],[十六],[除税成本],[报表除税毛利],[市场含税毛利]) VALUES (" + e.Keys[0].ToString() + "," + sd.ToString() + "," + csjs.ToString() + "," + yfyj.ToString() + ",'" + zcrq + "'," + sfyj.ToString() + "," + wp.ToString() + "," + pp.ToString() + "," + san.ToString() + "," + liu.ToString() + "," + zyrj.ToString() + "," + ss.ToString() + "," + sl.ToString() + "," + cscb + "," + csml + "," + hsml + ")";
                if (dt.Rows.Count > 0)
                {
                    updateSql = "update tbl_lrb set 税点=" + sd.ToString() + ",除税金额=" + csjs.ToString() + ",应付佣金=" + yfyj.ToString() + ",支付日期='" + zcrq + "',实付佣金=" + sfyj.ToString() + ",无票=" + wp.ToString() + ",普票=" + pp.ToString() + ",三=" + san.ToString() + ",六=" + liu.ToString() + ",十三=" + ss.ToString() + ",十六=" + sl.ToString() + ",自研软件=" + zyrj.ToString() + ",除税成本=" + cscb.ToString() + ",报表除税毛利=" + csml.ToString() + ",市场含税毛利=" + hsml.ToString() + " where DDID = " + e.Keys[0].ToString();
                }
                PL.SqlGetData(updateSql);
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.WriteXlsxToResponse("profit");
        }
    }
}